package com.hdyg.sixg.activity.application;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.OilPriceAdapter;
import com.hdyg.sixg.entry.OilPhoneBean;
import com.hdyg.sixg.mvp.Contrant.COil;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.POil;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 油卡充值
 * @time 2019/4/9 15:04
 */
public class RechargeOilCardActivity extends BaseActivity implements COil.IVOil {


    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.rv_type)
    RecyclerView rvType;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.tv_total)
    TextView tvTotal;
    @BindView(R.id.tv_fee)
    TextView tvFee;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private COil.IPOil mPresenter;
    private OilPriceAdapter mAdapter;
    private List<OilPhoneBean> datas;
    private String id,cardCode;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recharge_oil_card;
    }

    @Override
    protected void initView() {
        onClickListener();
        etMoney.setFocusable(false);
        etMoney.setFocusableInTouchMode(false);
        tvTopTitle.setText(R.string.application_2);
        rvType.setLayoutManager(new GridLayoutManager(mContext, 3));
        setTotal("0","0");
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());

        //立即充值
        btSure.setOnClickListener(v -> {
            cardCode = etPhone.getText().toString().trim();
            if (TextUtils.isEmpty(cardCode)){
                toastNotifyShort(R.string.oil_card_hint);
                return;
            }
            if (TextUtils.isEmpty(id)){
                toastNotifyShort(R.string.toast_oil_price);
                return;
            }
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            mPresenter.pGetApplyOil(BaseUrl.APPLY_OIL_URL,GetParamUtil.getApplyOilParam(cardCode,id,"1"));
        });
    }
    private void setTotal(String money,String miniFee){
        String str = String.format(getResources().getString(R.string.phone_total),money);
        String str2 = String.format(getResources().getString(R.string.recharge_phone),miniFee);
        tvTotal.setText(str);
        tvFee.setText(str2);
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.OIL_INIT_URL,GetParamUtil.getInitOilParam());
    }

    private void initAdapter() {
        mAdapter = new OilPriceAdapter(R.layout.item_oil_price, datas);
        rvType.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            etMoney.setText(datas.get(position).getAmount());
            setTotal(datas.get(position).getAll(),datas.get(position).getRate());
            id = datas.get(position).getId();
            for (int i = 0; i < datas.size(); i++) {
                if (i == position) {
                    datas.get(i).setIs_select(true);
                } else {
                    datas.get(i).setIs_select(false);
                }
            }
            mAdapter.replaceData(datas);
            mAdapter.notifyDataSetChanged();
        });
    }

    @Override
    protected void createPresenter() {
        mPresenter = new POil(this);
    }

    @Override
    public void vGetInitSuccess(List<OilPhoneBean> dataBean) {
        datas = dataBean;
        setTotal("0","0");
        initAdapter();
    }

    @Override
    public void vGetApplyOilSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        finish();
    }
}
